<template>
  <div>
    <el-row>
      <el-col
        v-for="(item,index) in filterList"
        :key="index"
        :span="4"
      >
        <template v-if="item.formType==='0'">
          <selectTree :param-type="'0002'" style="width:90%" :common-index="index" :show-label="item.showLabel" :item-url="item.url" :placeholder="item.placeholder" :common-values="item.value" @changeData="changeData" @clearSelect="clearSelect" />
        </template>
        <template v-if="item.formType==='1'">
          <el-input v-model="item.value" clearable style="width:90%" :placeholder="item.placeholder" />
        </template>
        <template v-if="item.formType==='2'">
          <el-select v-model="item.value" style="width:90%" filterable clearable :placeholder="item.placeholder" @focus="getSelectFouce(item.url,item.placeholder,index)">
            <el-option
              v-for="(item1,index1) in item.selectOption"
              :key="index1"
              :label="item1[item.showLabel]"
              :value="item1[item.showValue]"
            />
          </el-select>
        </template>
      </el-col>
      <el-col :span="8" style="text-align: center;">
        <div class="grid-content bg-purple-light">
          <el-button type="text" @click="onSubmit">
            搜索
          </el-button>
          <el-button
            type="text"
            @click="reset()"
          >
            重置
          </el-button>
          <el-button type="text" @click="downFile">
            下载
          </el-button>
          <el-button type="text" @click="createProject('create')">
            新增
          </el-button>
          <span style="margin-left:30px">
            <el-button size="small" @click="gotoMessage">个人中心</el-button>
          </span>
        </div>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import { Message } from 'element-ui';
import selectTree from '@/components/SelectTree/index.vue';
import { getCommonDataSource } from '@/api/reportCenter';
import { exportExcel } from '@/api/projectLibrary';
import { downloadFile } from '@/utils/commonMethods.js';
export default {
  components: {
    selectTree
  },
  data() {
    return {
      selectOption: [],
      // 存放选择的条件
      filterList: [
        // 0 是下拉树 1 是input 2 是下拉框
        {
          placeholder: '项目编码',
          formType: '1',
          value: '',
          symbol: 'ct',
          col: 'ISBNCODE'
        },
        {
          placeholder: '项目名称',
          formType: '1',
          value: '',
          symbol: 'ct',
          col: 'NAME'
        },
        {
          // 集中财力办大事
          url: 'option/findByType',
          placeholder: '集中财力办大事',
          formType: '2',
          value: '',
          symbol: 'eq',
          showLabel: 'text',
          showValue: 'id',
          col: 'FINANCIALMATTERS',
          selectOption: []
        },
        {
          url: 'option/listOptions',
          placeholder: '项目来源',
          formType: '2',
          value: '',
          symbol: 'eq',
          showLabel: 'text',
          showValue: 'label',
          col: 'NATIVE',
          selectOption: []
        }

      ],
      distriTypeOptions: [
        {
          value: '2',
          label: '不跟踪'
        },
        {
          value: '1',
          label: '跟踪'
        }
      ]
    };
  },
  created() {},
  methods: {
    gotoMessage() {
      this.$parent.drawer = true;
      this.$parent.checked = '';
      this.$parent.filterText = '';
      this.$parent.activeName = 'first';
      this.$parent.handleTabClick();
    },
    getSelectFouce(url, placeholder, index) {
      if (placeholder === '项目来源') {
        this.filterList[index].selectOption = [{
          text: '0 预算云项目',
          label: '0'
        }, {
          text: '1 本地新增',
          label: '1'
        }, {
          text: '2 已替换为预算云项目',
          label: '2'
        }];
      } else {
        getCommonDataSource({
          type: '0002',
          resultAsTree: true
        }, url).then(res => {
          if (res.success) {
            this.filterList[index].selectOption = res.data;
          } else {
            this.filterList[index].selectOption = [];
          }
        });
      }
    },
    changeData(row) {
      this.filterList[row.commonIndex].value = row.data[row.showLabel];
    },
    clearSelect(row) {
      this.filterList[row.commonIndex].value = '';
    },
    createProject(type) {
      const isLowest = this.$parent.$refs.enterpriseTree.isLowest;
      if (!isLowest) {
        Message({
          message: '请先选择底层单位',
          type: 'warning'
        });
      } else {
        this.$parent.$refs.enterpriseDialog.dialogVisible = true;
        this.$parent.$refs.enterpriseDialog.isDisable = false;
        this.$parent.$refs.enterpriseDialog.title = '新增项目';
      }
    },
    downFile() {
      const enterpriseGuid = this.$parent.$refs.enterpriseTree.enterpriseGuid;
      if (enterpriseGuid === '') {
        Message({
          message: '请先选择单位',
          type: 'warning'
        });
      } else if (this.$parent.count === 0) {
        Message({
          message: '数据为空,无法下载',
          type: 'warning'
        });
      } else {
        let filterList1 = [];
        if (enterpriseGuid === 'all') {
          filterList1 = [];
        } else {
          filterList1 = [{
            value: enterpriseGuid,
            col: 'ENTERPRISEGUID',
            symbol: 'in'
          }];
        }
        this.filterList.map(o => {
          filterList1.push({
            value: o.formType === '0' ? o.value !== '' ? o.value.split(' ')[0] : o.value : o.value,
            col: o.col,
            symbol: o.symbol
          });
        });
        exportExcel({
          columnList: this.$parent.columnList,
          filterList: filterList1,
          headerList: this.$parent.headerList,
          moneyType: '1',
          year: localStorage.getItem('chooseYear'),
          month: '',
          orderBy: '',
          pageType: '0',
          pageSize: this.$parent.pageSize,
          pageIndex: this.$parent.pageIndex
        }).then(res => {
          downloadFile(res, `项目库文件.xls`, '.xls');
        });

        // window.location.href = `${global_.BASE_URL}/produce/exportExcel?pageType=0&year=${localStorage.getItem('chooseYear')}&enterpriseGuid=${enterpriseGuid}`;
      }
    },
    reset() {
      this.filterList.map(o => {
        o.value = '';
      });
      if (this.$parent.$refs.enterpriseTree.isLowest) {
        this.onSubmit();
      } else {
        Message({
          message: '重置成功',
          type: 'success'
        });
      }
    },
    onSubmit() {
      this.$parent.pageSize = 50;
      this.$parent.pageIndex = 1;
      this.$parent.getData();
    }
  }
};
</script>
<style lang="scss" scoped>
.buttonClass {
  width: 80px;
  display: inline-block;
  // background: rgba(51, 119, 255, 1);
  border-radius: 4px;
}
</style>

